package com.njym.java.security.admin.rest;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njym.java.security.admin.biz.SupportBussinessBiz;
import com.njym.java.security.admin.biz.TravelBussinessRelationBiz;
import com.njym.java.security.admin.entity.SupportBussiness;
import com.njym.java.security.admin.entity.TravelBussinessRelation;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.msg.TableResultResponse;
import com.njym.java.security.common.rest.BaseController;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("travelBussinessRelation")
public class TravelBussinessRelationController extends BaseController<TravelBussinessRelationBiz,TravelBussinessRelation> {

    @Autowired
    private SupportBussinessBiz supportBussinessBiz;

    /**
     * 获取行程承办企业列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ResponseBody
    public Object getList(@RequestParam Map<String, Object> params){
        int page = Integer.parseInt(params.get("page").toString());
        int limit = Integer.parseInt(params.get("limit").toString());
        Page<Object> result = PageHelper.startPage(page,limit);
        String name = params.get("name").toString();
        String place = params.get("place").toString();
//        baseBiz.getList(name,place);
        supportBussinessBiz.getList(name, place);
        return new TableResultResponse<>(result.getTotal(),result.getResult());
    }

    @RequestMapping(value = "/addObj", method = RequestMethod.POST)
    @ResponseBody
    public Object addObj(@RequestBody String data){
        SupportBussiness entity = JSON.parseObject(data, SupportBussiness.class);
        entity.setAttr3( getPinYinHeadChar(JSON.parseObject(data).get("province").toString()).substring(0,1).toUpperCase() );
        try {
            supportBussinessBiz.insertSelective(entity);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            return new ObjectRestResponse<>().rel(false);
        }
    }

    @RequestMapping(value = "/editObj", method = RequestMethod.PUT)
    @ResponseBody
    public Object editObj(@RequestBody String data){
        SupportBussiness entity = JSON.parseObject(data, SupportBussiness.class);
        entity.setAttr3( getPinYinHeadChar(JSON.parseObject(data).get("province").toString()).substring(0,1).toUpperCase() );
        try {
            supportBussinessBiz.updateSelectiveById(entity);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            System.out.println(e);
            return new ObjectRestResponse<>().rel(false);
        }
    }



    @RequestMapping(value = "/deleteRelation", method = RequestMethod.GET)
    @ResponseBody
    public Object deleteRelation(Integer travelId, Integer bussnessId){
        try {
            TravelBussinessRelation entity = new TravelBussinessRelation();
            entity.setTravelId(travelId);
            entity.setBussinessId(bussnessId);
            baseBiz.delete(entity);
            return new ObjectRestResponse<>().rel(true);
        }catch (RuntimeException e){
            return new ObjectRestResponse<>().rel(false);
        }

    }


    /**
     * 中文转首字符
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }

}